package com.goods.controller.business;

import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business/consumer")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;


    /**
     * 获取所有物资去处
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/findConsumerList")
    //?pageNum=1&pageSize=10&name=
    public ResponseBean findConsumerList(@RequestParam("pageNum")int pageNum,
                                         @RequestParam("pageSize")int pageSize,
                                         @RequestParam("name") String name){
        try {
            PageVO<Consumer> pageVO = consumerService.findConsumerList(pageNum,pageSize,name);
            return ResponseBean.success(pageVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询失败");
        }
    }

    /**
     * 添加物资去处
     * @param consumer
     * @return
     */
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Consumer consumer){
        try {
            consumerService.add(consumer);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("添加失败");
        }
    }

    /**
     * 编辑回显
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        try {
            Consumer consumer = consumerService.edit(id);
            return ResponseBean.success(consumer);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("回显失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        try {
            consumerService.delete(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("删除失败");
        }
    }

    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody Consumer consumer){
        try {
            consumerService.update(consumer);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("修改失败");
        }
    }

    /**
     * 查询所有去向
     * @return
     */
    @GetMapping("/findAll")
    public ResponseBean findAll(){
        try {
            List<Consumer> list = consumerService.findAll();
            if (CollectionUtils.isEmpty(list)){
                return ResponseBean.error("查询失败");
            }
            return ResponseBean.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询失败");
        }
    }

}
